package com.network.config;

import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import com.network.entity.FileSave;
import com.network.util.Consumer;
import com.network.util.FileSaveFactory;
import com.network.util.Producer;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@Component
public class BeanConfig
{

    private Producer producer;

    @PostConstruct
    public void getDisruptor()
    {
        Executor executor= Executors.newCachedThreadPool();
        FileSaveFactory fileSaveFactory=new FileSaveFactory();
        Disruptor<FileSave> disruptor=  new Disruptor<FileSave>(fileSaveFactory
                , 1024,
                executor,
                ProducerType.MULTI,
                new BlockingWaitStrategy());
        disruptor.handleEventsWithWorkerPool(new Consumer<FileSave>(),new Consumer<FileSave>(),new Consumer<FileSave>());
        disruptor.start();
        RingBuffer<FileSave> ringBuffer=disruptor.getRingBuffer();
        producer=new Producer(ringBuffer);
    }
    @Bean
    public Consumer<FileSave> getConsumer()
    {
        return  new Consumer<FileSave>();
    }
    //填入消息
    public void sendAsyncEmail(FileSave sc) {
        producer.puthData(sc);
    }



}
